//
//  ViewController.swift
//  ios_alert
//
//  Created by 雨雪霏霏 on 2020/5/10.
//  Copyright © 2020 杨钟雷. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
    }
    override func viewDidAppear(_ animated: Bool) {
        practice2()
    }

    func practice1(){
        let alertController = UIAlertController(title: "this is a alert", message: nil, preferredStyle: UIAlertController.Style.alert)
        alertController.addTextField(configurationHandler: {
            (textFiled:UITextField!)-> Void in
            textFiled.placeholder = "this is a hint"
//            NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange), name: NSNotification.Name.t, object: <#T##Any?#>)
            NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange), name: UITextField.textDidChangeNotification, object: textFiled)

        })
        
        
        let cancleAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
        
        let okaction = UIAlertAction(title: "确定", style: .default, handler: {
            (acion:UIAlertAction!)-> Void in
            let login = (alertController.textFields?.first)! as UITextField
            let str = login.text
                        NotificationCenter.default.removeObserver(self, name: UITextField.textDidChangeNotification, object: nil)
        })
        okaction.isEnabled = false
        alertController.addAction(okaction)
        alertController.addAction(cancleAction)
        self.present(alertController, animated: true, completion: nil)
        
    }
    
    @objc func alertTextFieldDidChange(){
        let alertController = self.presentedViewController as! UIAlertController?
        if (alertController != nil) {
            let login = (alertController!.textFields?.first)! as UITextField
            let okaction  = alertController!.actions.last! as UIAlertAction
           
            
            if (!(login.text?.isEmpty)!) {
                okaction.isEnabled = true
                
            }else{
                okaction.isEnabled = false
            }
        }
        
    }
    
    
    func practice2(){
        
        weak var weakSelf = self // 弱引用
        let alertController = UIAlertController()
        alertController.view.tintColor = UIColor.red// 修改颜色
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let directMessagesAction = UIAlertAction(title: "私信", style: .default) { (action) in
            
        }
        let focusOnAction = UIAlertAction(title: "关注", style: .default) { (action) in
            
        }
        alertController.addAction(focusOnAction)
        alertController.addAction(directMessagesAction)
        alertController.addAction(cancelAction)
        weakSelf!.present(alertController, animated: true, completion: nil)
        
    }
    
}

